package com.issoft.studentresitor.controller;

import cn.hutool.core.bean.BeanUtil;
import com.issoft.studentresitor.domain.dto.UserDTO;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.pojo.User;
import com.issoft.studentresitor.domain.vo.UserVo;
import com.issoft.studentresitor.service.IStudentService;
import com.issoft.studentresitor.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "登录与注册")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IStudentService studentService;
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){
        log.info("用户登录：{}", userDTO);
        UserVo user = userService.login(userDTO);
        log.info("用户登录成功：{}", user);
        if (user == null) {
            log.info("用户登录失败：{}", user);
            return Result.error("用户名或密码错误");
        }
        return Result.success(user);
    }
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        log.info("用户注册：{}", user);
        userService.insertUser(user);
        log.info("用户注册成功：{}", user);
        return Result.success();
    }
    @ApiOperation("修该密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UserDTO userDTO){
        log.info("修改密码：{}", userDTO);
        int i=userService.updatePassword(userDTO);
        if (i==0) {
            log.info("修改密码失败：{}", userDTO);
            return Result.error("用户不存在");
        }
        log.info("修改密码成功：{}", userDTO);
        return Result.success("修改成功");
    }
    @ApiOperation("查询出未被分配的账号user_id")
    @PostMapping("/queryNoUserId")
    public Result queryNoUserId(){
        log.info("查询出未被分配的账号user_id");
        List<User> users = userService.queryNoUserIdStu();
        return Result.success(users);
    }
    @ApiOperation("根据user_id修改密码")
    @PostMapping("/updatePasswordByUserId")
    public Result updatePasswordByUserId(@RequestParam(value = "userId") Integer userId,
                                         @RequestParam(value = "password") String password)
        {
        log.info("根据user_id修改密码：{}", userId);
        int i=userService.updatePasswordByUserId(userId,password);
        if (i==0) {
            log.info("修改密码失败：{}", userId);
            return Result.error("用户不存在");
        }
        log.info("修改密码成功：{}", userId);
        return Result.success("修改成功");
    }

}
